// miniprogram/pages/gwCar/gwCar.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        timeList: [],
        typeList: [],
        checkList: [],
        del: false,
        delList: []
    },
    onChange(event) {
        this.setData({
            checkList: event.detail
        });
    },
    all() {
        if (this.data.checkList.length == this.data.list.length) {
            this.setData({
                checkList: []
            })
        } else {
            let temporary = []
            this.data.list.forEach((element, index) => {
                temporary.push(index.toString())
            });
            this.setData({
                checkList: temporary
            })
        }

    },
    delItem() {
        let that = this
        if (this.data.del == true && this.data.checkList.length == 0) {
            this.setData({
                del: false
            })
        } else if (this.data.del == false) {
            this.setData({
                del: true
            })
        } else if (this.data.del == true && this.data.checkList.length != 0) {
            wx.showModal({
                title: '提示',
                content: '确定取消交易',
                success(res) {
                    if (res.confirm) {
                        wx.showLoading({
                            mask: true,
                            title: '加载中',
                        })
                        let list = []
                        that.data.checkList.forEach(index => {
                            list.push({
                                // type: this.data.typeList[index],
                                id: that.data.list[index]._id
                            })
                        });

                        wx.cloud.callFunction({
                            name: 'changeGoodsList',
                            data: {
                                list: list,
                                type: "gwCar_del"
                            }
                        }).then((data) => {
                            if (data.result == "ok") {
                                that.setData({
                                    checkList: []
                                })
                                that.getList()
                            } else {
                                wx.showToast({
                                    title: '错误',
                                    icon: 'none',
                                    image: '/pages/images/icon/error.png',
                                    duration: 2000
                                })
                            }
                        })
                    }
                }
            })


        }

    },
    gotoDetails(e) {
        // console.log(e.currentTarget.dataset);
        let status_ = ""
        if (e.currentTarget.dataset.type == "have") {
            status_ = "4"
        } else {
            status_ = "3"
        }
        wx.navigateTo({
            url: '/pages/homePage/usedGoods/details/details',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('to_details', {
                    mes: e.currentTarget.dataset.item,
                    status: status_
                })
            }
        })
    },
    getList() {
        wx.cloud.callFunction({
            name: 'getgwCar',
        }).then(data => {
            let new_1 = []
            let new_2 = []
            let new_3 = []
            data.result.forEach(element => {
                new_1.push(element.goods)
                new_2.push(element.time)
                new_3.push(element.type)
            });
            this.setData({
                list: new_1.reverse(),
                timeList: new_2.reverse(),
                typeList: new_3.reverse()
            })
            wx.hideLoading()
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.showLoading({
            mask: true,
            title: '加载中',
        })
        this.setData({
            checkList: [],
            del: false
        })
        this.getList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})